package com.ying.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ying.domain.dto.UserFormDTO;
import com.ying.domain.po.User;
import com.ying.domain.query.UserQuery;
import com.ying.domain.vo.UserVO;
import com.ying.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liujunjie
 * @Description description
 * @Date 2024-09-07 16:12
 **/
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Tag(name = "用户管理")
public class UserController {

    private final IUserService userService;

    @Operation(summary = "新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userDto) {
        //将DTO 拷贝到 po
        User user = BeanUtil.copyProperties(userDto, User.class);
        userService.save(user);
    }

    @Operation(summary = "根据用户id删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUser(@Parameter(ref = "用户id") @PathVariable("id") Long id) {
        userService.removeById(id);
    }

    @Operation(summary = "根据用户id查询用户接口")
    @GetMapping("/{id}")
    public UserVO getUser( @PathVariable("id") Long id) {
        return  userService.queryUserAndAddressById(id) ;
    }

    @Operation(summary = "根据用户id批量查询用户接口")
    @GetMapping
    public List<UserVO> getUser( @RequestParam("ids") List<Long> ids) {
        return   userService.queryUserAndAddressByIds(ids) ;
    }

    @Operation(summary = "扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductBalance(
            @Parameter(description = "ss") @PathVariable("id") Long id,
            @Parameter(description = "扣减的金额") @PathVariable("money") Integer money) {
        userService.deductBalance(id, money);
    }

    @Operation(summary = "根据复杂条件查询用户接口")
    @GetMapping("list")
    public List<UserVO> queryUsers(UserQuery query) {
        List<User> users = userService.queryUsers(
                query.getName(), query.getStatus()
                , query.getMinBalance(), query.getMaxBalance());
        return BeanUtil.copyToList(users, UserVO.class);
    }
}
